package com.leetcode.partition21;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/20 16:10
 */
public class LC2039网络空闲的时刻 {

    private static final int N = 100010, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], e = new int[2 * N], ne = new int[2 * N], dist = new int[N];
    private static int[] q = new int[N];
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int n = Integer.parseInt(ss[0]), m = Integer.parseInt(ss[1]);
        int[][] edges = new int[m][2];
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            edges[i] = new int[]{a, b};
        }
        int[] patience = new int[n];
        ss = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) patience[i] = Integer.parseInt(ss[i]);
        writer.write(networkBecomesIdle(edges, patience) + "\n");
        writer.flush();
    }

    public static int networkBecomesIdle(int[][] edges, int[] patience) {
        n = patience.length;
        m = edges.length;
        for (int i = 0; i < n; i++) {
            h[i] = -1;
            dist[i] = INF;
        }
        for (int[] edge : edges) {
            add(edge[0], edge[1]);
            add(edge[1], edge[0]);
        }
        int hh = 0, tt = 0, ans = 0;
        q[0] = 0;
        dist[0] = 0;
        while (hh <= tt) {
            int u = q[hh++];
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dist[v] != INF) continue;
                dist[v] = dist[u] + 1;
                q[++tt] = v;
            }
        }
        for (int i = 1; i < n; i++) {
            int d = 2 * dist[i], t = patience[i];
            if (d <= t) ans = Math.max(ans, d);
            else ans = Math.max(ans, (d - 1) / t * t + d);
        }
        return ans + 1;
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
